

//var baseUrl = 'https://app.artshanghaifair.com/'
var baseUrl = window.location.protocol+"//"+window.location.host+'/'

/**
 * 获取token
 * @returns {jQuery|*}
 */
function getToken() {
    //alert( $.cookie('token'));
    return localStorage.getItem('token')
}

/**
 * 获取用户信息
 * @returns {null|any}
 */
function getUserInfo() {
    var user = localStorage.getItem('userInfo')
    if(user){
        return JSON.parse(user)
    }
    return  null;
}

/**
 * 获取url参数
 * @param variable
 * @returns {string|boolean}
 */
function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
    }
    return(false);
}

/**
 * 重新登录
 * @param url
 */
function relogin(url) {

    localStorage.removeItem('userInfo');
    localStorage.removeItem('token');
    if(url){
        window.location.href = '/login/login?returnUrl='+ encodeURIComponent(url);
    }
    else{
        window.location.href = '/login/login';
    }
}

/**
 * 检查登录 跳转
 * @param url
 */
function checkLogin(url) {
    debugger;
    if(!url){
        return;
    }
    var token = getToken();
    if(token){
        window.location.href = url
    }else {
        relogin(url)
    }
}

/**
 * 登出
 * @param *
 */
function logout() {

    var r = confirm("是否要登出");
    // 获得
    if (r==true){
        localStorage.removeItem('userInfo')
        localStorage.removeItem('token')
        changeState();
    }
}

/**
 * funcUrlDel
 * @param name
 * @returns {string|any}
 */
function removeQueryVariable(name) {
    var loca = window.location;
    var baseUrl = loca.origin + loca.pathname + "?";
    console.log('loca.hash',loca)
    var query = decodeURIComponent(loca.search.split('?')[1]);
    if(!query){
        return loca
    }
    if (loca.href.indexOf(name) > -1) {
        var obj = {}
        console.log('query',query)
        var arr = query.indexOf('&')>-1?query.split("&"):[query];
        for (var i = 0; i < arr.length; i++) {
            arr[i] = arr[i].split("=");
            obj[arr[i][0]] = arr[i][1];
        };
        delete obj[name];
        var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g, "").replace(/\:/g, "=").replace(/\,/g, "&");
        return url
    };
}

/**
 * 更新
 * @param *
 */
function changeState(){
    try{
        var user = getUserInfo()
        if(user){
            $('.user-logined').show()
            $('.user-login').hide()
            $(".user-name").html(user.nickName==''?'匿名':user.nickName)
        }else{
            $('.user-logined').hide()
            $('.user-login').show()
            $(".user-name").html('登录/注册')
        }
    }
    catch (e) {

    }
}

/**
 * 过滤input中其他语法
 * @param *
 */
 function replaceStr (param){
    var rules = /script|<?php |select|update|delete|truncate|join|union|exec|insert|drop|count/gi
    var str = param.replace(rules,function(sMatch){
    			return sMatch.replace(/./g,"")
    		})
    return str
 }
/**
 * 搜索
 * @param *
 */
function search(){

    $("#form-search-input").keydown(function(event){

        if (event.keyCode == 13) {

            var str = replaceStr($("#form-search-input").val())
            if($(".search-box-filter").html()==='艺术品'){
                window.location.href = '/works/index?name='+ str
                return;
            }
            if($(".search-box-filter").html()==='艺术家'){
                window.location.href = '/artist/search?name='+ str
                return;
            }
            if($(".search-box-filter").html()==='艺术机构'){
                window.location.href = '/exhibition/exhibition?name='+ str
                return;
            }
        }
    });


    $(".search-btn").click(function () {
     var str = replaceStr($("#form-search-input").val())
        if($(".search-box-filter").html()==='艺术品'){
            window.location.href = '/works/index?name='+str
            return;
        }
        if($(".search-box-filter").html()==='艺术家'){
            window.location.href = '/artist/search?name='+str
            return;
        }
        if($(".search-box-filter").html()==='艺术机构'){
            //$(this).html('艺术品')
            window.location.href = '/exhibition/exhibition?name='+str
            return;
        }
    })


    $(".search-box-filter").click(function () {
        if($(this).html()==='艺术品'){
            $(this).html('艺术家')
            return;
        }
        if($(this).html()==='艺术家'){
            $(this).html('艺术机构')
            return;
        }
        if($(this).html()==='艺术机构'){
            $(this).html('艺术品')
            return;
        }
    })
}

// 百度siteMap
function postSiteMap() {

    var host = window.location.protocol+"//"+window.location.host ;

    //百度
    $.ajax({
        type: "POST",
        url: "https://data.zz.baidu.com/urls?site="+host+"&token=LoN1BEVMmBai3qqf",
        contentType: "text/plain",
        data: window.location.href,
        success: function (res) {
            debugger;
        },
        error: function (res) {
        }
    });
}


function urlUpdateParams (url, name, value) {
    var r = url;
    if (r != null && r != 'undefined' && r != "") {
        value = encodeURIComponent(value);
        var reg = new RegExp("(^|)" + name + "=([^&]*)(|$)");
        var tmp = name + "=" + value;
        if (url.match(reg) != null) {
            r = url.replace(eval(reg), tmp);
        }
        else {
            if (url.match("[\?]")) {
                r = url + "&" + tmp;
            } else {
                r = url + "?" + tmp;
            }
        }
    }
    return r;
}


$(function () {

    //提交siteamp
    try{
        postSiteMap();
    } catch (e) {

    }

    //更改状态
    changeState();
    //搜索
    search();

    $(window).scroll(function() {

        if ($(document).scrollTop() > 70) {
            $('nav').addClass('nav-shrink');
        } else {
            $('nav').removeClass('nav-shrink');
        }
    });
//    debugger;
    var lang = $.cookie('org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE');
    if(lang =='en-US'){
        $('#btn-lang').html('中文');
        // if(window.location.href.indexOf('&')!==-1){
        //     var url = window.location.href + '&_lang=zh_CN'
        //     $('#btn-lang').attr('href',url);
        // }else{
        //     var url = window.location.href + '?_lang=zh_CN'
        //     $('#btn-lang').attr('href',url);
        // }
    }else{
        $('#btn-lang').html('EN');
        //  $.cookie('org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE','zh-CN')
        // if(window.location.href.indexOf('&')!==-1){
        //     var url = window.location.href + '&_lang=en_US'
        //     $('#btn-lang').attr('href',url);
        // }else{
        //     var url = window.location.href + '?_lang=en_US'
        //     $('#btn-lang').attr('href',url);
        // }
    }

    $('#btn-lang').click(function () {
        var lang = $.cookie('org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE');
        if(lang =='en-US'){
            $
            $.cookie('org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE','zh-CN');
            window.location.reload()
        }else{

            $.cookie('org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE','en-US');
            window.location.reload()
        }
    })



    $("#goTop").click(function () {
        debugger;
        $("html,body").animate({scrollTop:0},1200);
        return false;
    })

    $(".btn-search-top").click(function () {
        var h=$(this).offset();
        $(".search-box").show()
        $(".search-box").css({left: (h.left-380)+'px',width:'0px',display:'flex'})
        $(".search-box").animate({width:'380px'},300);
    })

    $(".close-btn").click(function () {
        $(".search-box").animate({width:'0px'},300,function () {
            $(".search-box").hide()
        });
    })

    $('#chartBtn').click(function () {
        $('.yunque_chat-btn').trigger('click')
    })




})


